<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="读卡器编码" prop="cardNo">
      <el-input v-model="dataForm.cardNo" placeholder="读卡器编码"></el-input>
    </el-form-item>
    <el-form-item label="读卡器型号" prop="cardType">
      <el-input v-model="dataForm.cardType" placeholder="读卡器型号"></el-input>
    </el-form-item>
    <el-form-item label="对应门禁编号" prop="doorNo">
      <el-input v-model="dataForm.doorNo" placeholder="对应门禁编号"></el-input>
    </el-form-item>
    <el-form-item label="所属区域" prop="area">
      <el-input v-model="dataForm.area" placeholder="所属区域"></el-input>
    </el-form-item>
    <el-form-item label="读卡器名称" prop="cardName">
      <el-input v-model="dataForm.cardName" placeholder="读卡器名称"></el-input>
    </el-form-item>
    <el-form-item label="读卡器地址" prop="cardIp">
      <el-input v-model="dataForm.cardIp" placeholder="读卡器地址"></el-input>
    </el-form-item>
    <el-form-item label="端口" prop="cardPort">
      <el-input v-model="dataForm.cardPort" placeholder="端口"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="心跳时间" prop="heartTime">
      <el-input v-model="dataForm.heartTime" placeholder="心跳时间"></el-input>
    </el-form-item>
    <el-form-item label="读卡器状态" prop="cardStatus">
      <el-select v-model="dataForm.cardStatus" placeholder="请选择">
        <el-option
          v-for="item in doorStatus"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="对应门禁" prop="doorName">
      <el-input v-model="dataForm.doorName" placeholder="对应门禁"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/hik/doorcard'

    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        doorStatus: [
          {
            value: '0',
            label: '在线'
          },
          {
            value: '1',
            label: '离线'
          }],
        dataForm: {
          id: '',

          cardNo: '',

          cardType: '',

          doorNo: '',

          area: '',

          cardName: '',

          cardIp: '',

          cardPort: '',

          username: 0,
          password: '',

          createTime: '',

          heartTime: '',

          updateTime: '',

          delFlag: '',

          cardStatus: '',

          doorName: '',

        },
        dataRule: {
          id: [
            { required: true, message: 'id不能为空', trigger: 'blur' }
          ],

          cardNo: [
            { required: true, message: '读卡器编码不能为空', trigger: 'blur' }
          ],

          cardType: [
            { required: true, message: '读卡器型号不能为空', trigger: 'blur' }
          ],

          doorNo: [
            { required: true, message: '对应门禁编号不能为空', trigger: 'blur' }
          ],

          area: [
            { required: true, message: '所属区域不能为空', trigger: 'blur' }
          ],

          cardName: [
            { required: true, message: '读卡器名称不能为空', trigger: 'blur' }
          ],

          cardIp: [
            { required: true, message: '读卡器地址不能为空', trigger: 'blur' }
          ],

          cardPort: [
            { required: true, message: '端口不能为空', trigger: 'blur' }
          ],

          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],

          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],

          heartTime: [
            { required: true, message: '心跳时间不能为空', trigger: 'blur' }
          ],

          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],

          delFlag: [
            { required: true, message: '0 正常 其余删除不能为空', trigger: 'blur' }
          ],

          cardStatus: [
            { required: true, message: '读卡器状态不能为空', trigger: 'blur' }
          ],

          doorName: [
            { required: true, message: '对应门禁不能为空', trigger: 'blur' }
          ],

        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.username = id || 0;
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.username) {
            getObj(this.dataForm.username).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.username) {
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      }
    }
  }
</script>
